<?php

namespace App\Http\Controllers\API;

use App\Models\Goods;
use App\Models\Label;
use App\Repositories\ArticleRepository;
use App\Repositories\GoodsRepository;
use App\ZL\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class GoodsAPIController extends ApiBaseController
{
    protected $goods_res;
    protected $model_name = 'App\Models\Goods';
    protected $model_load_array = [
    ];
    protected $search_field_array = [
        'category_id','id'
    ];

    public function __construct(GoodsRepository $goodsRepository)
    {
        $this->goods_res = $goodsRepository;
    }

    //颜色、规格、克重、工艺、材质、关键词


    public function authLimit(&$model)
    {
        $res = [['is_delete',0]];
        $color_id = \request('color_id', null);
        $like = \request('like', false);
        $color_title = \request('color_title', null);
        $title = \request('title', null);
        $length_max = \request('length_max', null);
        $length_min = \request('length_min', null);
        $wide_max = \request('wide_max', null);
        $wide_min = \request('wide_min', null);
        $gram_weight_max = \request('gram_weight_max', null);
        $gram_weight_min = \request('gram_weight_min', null);
        $texture = \request('texture', null);
        $technology = \request('technology', null);
        $labels = \request('labels', null);

        if($labels!==null){
            if(is_string($labels))
                $labels = explode(',',$labels);

            if(count($labels)>0){
                $goods_ids = $this->goods_res->getLimitGoods($labels);
                $model = $model->with('labels')->whereIn('id',$goods_ids)->whereHas('labels', function ($query)use($labels) {
                        $query->whereIn('label_id', $labels);
                    });
            }
        }
        if($color_title!==null){
            $color_ids = DB::table('colors')->where('title', 'like',"%$color_title%")->pluck('id')->toArray();
            $goods_id = DB::table('goods_color')->whereIn('color_id', $color_ids)->pluck('goods_id')->toArray();
            $model = $model->whereIn('id',$goods_id);
        }else if($color_id!==null){
            $goods_id = DB::table('goods_color')->where('color_id', $color_id)->pluck('goods_id');
            $model = $model->whereIn('id',$goods_id);
        }
        if($title!==null && $title!==''){
            $user = Auth::user();
            if($user->lately_titles){
                $lately_titles = explode(',',$user->lately_titles);
                if(count($lately_titles)==10){
                    if(array_search($title,$lately_titles)===false){
                        array_unshift($lately_titles,$title);
                        array_pop($lately_titles);
                    }
                }else{
                    if(array_search($title,$lately_titles)===false)
                        array_unshift($lately_titles,$title);
                }
                $user->lately_titles = implode(',',$lately_titles);
            }else{
                $user->lately_titles = $title;
            }
            $user->save();
            $model = $model->where(function ($query) use ($title){
                $query->where('title', 'like',"%$title%")->orWhere('sn', 'like',"%$title%")->orWhere('sn', 'like',"%$title%");
                $labels = Label::where('title','like',"%$title%")->pluck('id')->toArray();
                if(count($labels)>0){
                    $query->with('labels')->orWhereHas('labels', function ($query1)use($labels) {
                        $query1->whereIn('label_id', $labels);
                    });
                }
            });
        }
        if($texture!==null){
            $model = $model->where('texture', 'like',"%$texture%");
        }
        if($technology!==null){
            $model = $model->where('technology', 'like',"%$technology%");
        }
        if($length_max!==null){
            $model = $model->where('length',"<=",$length_max);
        }
        if($length_min!==null){
            $model = $model->where('length',">=",$length_min);
        }
        if($wide_max!==null){
            $model = $model->where('wide',"<=",$wide_max);
        }
        if($wide_min!==null){
            $model = $model->where('wide',">=",$wide_min);
        }
        if($gram_weight_max!==null){
            $model = $model->where('gram_weight',"<=",$gram_weight_max);
        }
        if($gram_weight_min!==null){
            $model = $model->where('gram_weight',">=",$gram_weight_min);
        }
        if($like){
            $model = $model->whereIn('id',DB::table("goods_like")->where('user_id',\request('uid'))->pluck('goods_id')->toArray());
        }
        if(\request('uid')){
            $goods_check_ids = $this->goods_res->getLimitGoods();
            $model = $model->whereIn('id',$goods_check_ids);
        }
        return $res;
    }


    public function _after_get($data)
    {
        $data->load('category');
        $data->load('colors');
        $data->load('products');
        $data->load('articles');
        $data->load('slideshows');
        $user_id = Auth::id()?Auth::id():\request('uid');
        foreach ($data as &$v) {
            $like = DB::table('goods_like')->where([
                ['user_id',$user_id],
                ['goods_id',$v->id],
            ])->first();
            if($like && $user_id)
                $v->like = 1;
            else
                $v->like = 0;
        }
        return $data;
    }

    public function _after_find(&$data)
    {
        $data->read_num++;
        $data->save();
        $data->category;
        $data->colors;
        $data->products;
        $data->articles;
        $data->slideshows;
        $user_id = Auth::id()?Auth::id():\request('uid');
        if($user_id){
            $like = DB::table('goods_like')->where([
                ['user_id',$user_id],
                ['goods_id',$data->id],
            ])->first();
        }

        if(isset($like) && $user_id)
            $data->like = 1;
        else
            $data->like = 0;

        foreach ($data->products as &$product) {
//            $data->read_num++;
//            $data->save();
            $product->category;
            $product->colors;
            $product->products;
            $product->articles;
            $product->slideshows;
            $user_id = Auth::id()?Auth::id():\request('uid');
            if($user_id){
                $like = DB::table('goods_like')->where([
                    ['user_id',$user_id],
                    ['goods_id',$data->id],
                ])->first();
            }

            if(isset($like) && $user_id)
                $product->like = 1;
            else
                $product->like = 0;
        }
    }

    public function checkShow($id,$request)
    {
//        $goods_check_ids = $this->goods_res->getLimitGoods();
//        if(array_search($id,$goods_check_ids)!==false){
//            return null;
//        }else{
//            return '没有权限，无法查看商品详情';
//        }
    }


    //排序
    protected function modelGetSort(&$model)
    {
        $sortBy = request('sortBy','sort');
        $orderBy = request('orderBy','asc');

        $model = $model->when($sortBy, function ($query) use ($sortBy,$orderBy){
            return $query->orderBy($sortBy,$orderBy);
        }, function ($query) use ($orderBy){
            return $query->orderBy('id',$orderBy);
        });
        return $model;
    }
}
